using System;
using System.Diagnostics;
using CKS.EBE.API;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace CKS.EBE.WebControls
{
    public class PostList : BaseQueryControl
    {
        private bool? _TrimPostBody;

        public bool? TrimPostBody
        {
            get { return _TrimPostBody ?? BlogContext.Current.Settings.TrimPostBodyOnLists; }
            set { _TrimPostBody = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            string listRootFolderName = Localization.GetListName(Localization.BlogList.Posts, Web.Language);
            try
            {
                ViewFieldsXML += "<FieldRef Name='Body'/><FieldRef Name='NumComments'/><FieldRef Name='PublishedDate'/><FieldRef Name='PostCategory' Nullable='True'/><FieldRef Name='FileDirRef'/><FieldRef Name='BlogTitleForUrl'/><FieldRef Name='CategoriesAsText' Nullable='True' /><FieldRef Name='NumLinkbacks' Nullable='TRUE'/>";
                QueryXML = "<OrderBy><FieldRef Name='PublishedDate' Ascending='FALSE'/></OrderBy>";

                SPList postListData = Helper.GetListByFolderName(Web, listRootFolderName);

                if (!string.IsNullOrEmpty(HttpContext.Current.Request["c"]))
                {
                    // filter for category from querystring and only show published posts
                    QueryXML = string.Format("<Where><And>" +
                                             "<Eq><FieldRef Name='Categories'/><Value Type='Text'>{0}</Value></Eq>" +
                                             "<Leq><FieldRef Name='PublishedDate' /><Value Type='DateTime'>{1}</Value></Leq>" +
                                             "</And></Where>",
                                             HttpContext.Current.Request["c"], SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now)) + QueryXML;
                }
                else if (!string.IsNullOrEmpty(HttpContext.Current.Request["m"]))
                {
                    // filter for posts in month of day
                    var dtMonthStart = new DateTime(int.Parse(Page.Request["y"]), int.Parse(Page.Request["m"]), 1, 0, 0, 0);
                    string sStart = SPUtility.CreateISO8601DateTimeFromSystemDateTime(dtMonthStart.AddMinutes(-1));
                    DateTime endDate = dtMonthStart.AddMonths(1).AddMinutes(1);
                    // only show published posts
                    if (endDate > DateTime.Now) endDate = DateTime.Now;
                    string sEnd = SPUtility.CreateISO8601DateTimeFromSystemDateTime(endDate);

                    QueryXML = string.Format("<Where><And><Leq><FieldRef Name='PublishedDate'/><Value Type='DateTime'>{0}</Value></Leq><Gt><FieldRef Name='PublishedDate'/><Value Type='DateTime'>{1}</Value></Gt></And></Where>", sEnd, sStart) + QueryXML;
                }
                else
                {
                    // filter for published posts
                    QueryXML = string.Format("<Where><Leq><FieldRef Name='PublishedDate' /><Value Type='DateTime'>{0}</Value></Leq></Where>",
                                             SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Now)) + QueryXML;
                }

                ListsXML = string.Format("<Lists><List ID='{0}'/></Lists>", postListData.ID);// oPost.ID);

                Transform.XslArguments.AddParam("TrimPosts", "", TrimPostBody);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
                SPUtility.TransferToErrorPage(string.Format("Error in OnInit (PostList) for list '{0}': {1}", listRootFolderName, ex.Message));
            }
        }
    }
}
